<?php
/*
说明：
需求参数--$quizid：在mdl_quiz表中某个测试的id
		 $studentanswer:学生传来的答案id数组
返回--$status：false/true
	 $msg：提示信息
	 $data：数组（包含每道题实际得分和正确答案）以及成绩/总分（仅限单选题目）

*/

session_start();//session开始标签，在最顶部写。
/*---------------------一、PHP文件引用----------------------------------*/
require_once 'response2.php';
require_once 'connect_config.php';
require('../config.php');

/*---------------------二、数据库连接--------------------------------*/
$conn = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME,DB_PORT) or die('数据库连接失败');
mysqli_query($conn,"SET NAMES utf8");

/*---------------------三、变量定义--------------------------------*/
$status = false;
$msg = '';
$data = array();
$maxmark=0;
$totalmark=0;
$i=0;
$rightanswer=array();
$mark=array();

$studentanswer=$_GET['studentanswer'];//学生传来的答案id数组
$quizid=$_GET['quizid'];
$token=$_GET['token'];
/*
//测试数据
$quizid=2;
$token='f47ad7d1e02be1156bf70e786c25cec5';
$studentanswer=array('23','6','8','10','11','13','35','37','39','41');
*/
if(!$token)
{
	$username=$_SESSION["username"];	
}

/*---------------四、用户ID（确保登录用户才能获取资源）------------------*/	
if($token)
{
	$query1 = "select * from mdl_token where token='$token'";
	$data1= mysqli_query($conn, $query1);
	if($row1 = mysqli_fetch_array($data1))
	{
		$userid=$row1['user_id'];
		//echo "userid success<br>";
	}
	else
	{
		$status = false;
		$msg = 'cannot find userid in mdl_token';
		echo Response::json($status,$msg,$data);
	}
}
else if($username)
{
	$query1 = "select * from mdl_user where username='$username'";
	$data1= mysqli_query($conn, $query1);
	if($row1 = mysqli_fetch_array($data1))
	{
		$userid=$row1['id'];
		//echo "userid success<br>";
	}
	else
	{
		$status = false;
		$msg = 'cannot find userid in mdl_token';
		echo Response::json($status,$msg,$data);
	}
}
/*---------------五、获取测试内容------------------*/	
if($userid)
{
	$query2 = "select * from mdl_quiz_slots where quizid='$quizid'";
	$data2= mysqli_query($conn, $query2);
	while($row2 = mysqli_fetch_array($data2))//找到指定的quizid对应的所有questionid
	{
		$id=$row2['questionid'];
		$maxmark=$maxmark+$row2['maxmark'];//统计总分
		$query3 = "select * from mdl_question_answers where question='$id' and fraction='1.0000000'";//找到某个question对应的正确答案
		$data3= mysqli_query($conn, $query3);
		$row3 = mysqli_fetch_array($data3);
		$rightanswer[$i]=$row3['id'];
		if($studentanswer[$i]==$rightanswer[$i])
		{
			$data[$i]['questionnumber']=$i+1;//记录题序
			$totalmark=$totalmark+$row2['maxmark'];	
			$data[$i]['mark']=$row2['maxmark'];//记录每道题实际得分
			$arr=array('<b>','<p>','<br>','<sub>','<u>','<i>','<strike>','<sup>','&nbsp;','</p>','</b>','</br>','</sub>','</u>','</i>','</strike>','</sup>');
			$row3['answer']=str_replace($arr,"",$row3['answer']);
			$data[$i]['rightanswer']=$rightanswer[$i].$row3['answer'];//记录每道题正确答案
		}
		else
		{
			$data[$i]['questionnumber']=$i+1;//记录题序
			$data[$i]['mark']=0;
			$arr=array('<b>','<p>','<br>','<sub>','<u>','<i>','<strike>','<sup>','&nbsp;','</p>','</b>','</br>','</sub>','</u>','</i>','</strike>','</sup>');
			$row3['answer']=str_replace($arr,"",$row3['answer']);
			$data[$i]['rightanswer']=$rightanswer[$i].$row3['answer'];//记录每道题正确答案
		}		
		$i++;
	}
	$status =true;
	$msg = 'Your totalmark/maxmark is: '.$totalmark.'/'.$maxmark;
	//$data=$data1.$totalmark.'/'.$maxmark;//最后的返回格式为“数组 总分/满分”
	//$data=$data1;
	echo Response::json($status,$msg,$data);
}




?>